package com.hx.zhg.util;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Tree工具类
 *
 * @author XINFENG
 */
public class TreeUtil {
    private String _id;
    private String _text;
    private String _preId;
    private List<Node> tree;

    public TreeUtil(String _id, String _text, String _preId) {
        this._id = _id;
        this._text = _text;
        this._preId = _preId;
        this.tree = new ArrayList<Node>();
    }

    //使用指针的方式配置树形结构
    public List<Node> convertTree(List list) {
        list = new ArrayList(list);//简单克隆
        //转换根级节点
        Iterator rootiter = list.iterator();
        while (rootiter.hasNext()) {
            Object obj = rootiter.next();
            Map row = obj instanceof Map ? (Map) obj : MapUtilExt.convertBean(obj);
            if (StringUtilExt.isBlank(row.get(_preId)) || "".equals(row.get(_preId)) || "0".equals(row.get(_preId) + "")) {
                Node node = new Node(row.get(_id).toString(), row.get(_text).toString());
                node.setData(row);
                tree.add(node);
                rootiter.remove();
            }
        }

        List<Node> todo = new ArrayList<Node>();
        todo.addAll(tree);
        while (todo.size() > 0) {
            Node node = todo.remove(0);
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Object obj = iter.next();
                Map row = obj instanceof Map ? (Map) obj : MapUtilExt.convertBean(obj);
                if (row.get(_preId).equals(node.getId()) || (row.get(_preId) + "").equals(node.getId())) {
                    Node child = new Node(row.get(_id).toString(), row.get(_text).toString());
                    child.setData(row);
                    node.setExpanded(true);
                    if (row.get("checked") != null && (Boolean) row.get("checked")) {
                        child.setExpanded(true);
                    }
                    if (node.getChildren() != null) {
                        node.getChildren().add(child);
                    } else {
                        List<Node> children = new ArrayList<Node>();
                        children.add(child);
                        node.setChildren(children);
                    }
                    todo.add(child);
                }
            }
        }
        return tree;
    }

    //使用指针的方式配置树形结构
    public List<Node> convertTree(List list, String rootNode) {
        list = new ArrayList(list);//简单克隆
        //转换根级节点
        Iterator rootiter = list.iterator();
        while (rootiter.hasNext()) {
            Object obj = rootiter.next();
            Map row = obj instanceof Map ? (Map) obj : MapUtilExt.convertBean(obj);
            if (rootNode.equals(row.get(_id).toString())) {
                Node node = new Node(row.get(_id).toString(), row.get(_text).toString());
                node.setData(row);
                tree.add(node);
                rootiter.remove();
                break;
            }
        }

        List<Node> todo = new ArrayList<Node>();
        todo.addAll(tree);
        while (todo.size() > 0) {
            Node node = todo.remove(0);
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Object obj = iter.next();
                Map row = obj instanceof Map ? (Map) obj : MapUtilExt.convertBean(obj);
                if (row.get(_preId).equals(node.getId()) || (row.get(_preId) + "").equals(node.getId())) {
                    Node child = new Node(row.get(_id).toString(), row.get(_text).toString());
                    child.setData(row);
                    node.setExpanded(true);
                    if (row.get("checked") != null && (Boolean) row.get("checked")) {
                        child.setExpanded(true);
                    }
                    if (node.getChildren() != null) {
                        node.getChildren().add(child);
                    } else {
                        List<Node> children = new ArrayList<Node>();
                        children.add(child);
                        node.setChildren(children);
                    }
                    todo.add(child);
                }
            }
        }
        return tree;
    }

    public class Node {
        private String id;
        private String label;
        private String state;
        private boolean expanded;
        private Map data;
        private List<Node> children;

        public Node(String id, String label) {
            this.id = id;
            this.label = label;

        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public boolean isExpanded() {
            return expanded;
        }

        public void setExpanded(boolean expanded) {
            this.expanded = expanded;
        }

        public Map getData() {
            return data;
        }

        public void setData(Map data) {
            this.data = data;
        }

        public List<Node> getChildren() {
            return children;
        }

        public void setChildren(List<Node> children) {
            this.children = children;
        }

        @Override
        public String toString() {
            return "NODE:[" + this.id + "," + this.label + "," + this.children + "]";
        }
    }
}
